博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javaweb 一个Servlet 处理多个请求(运用反射机制)
阅读量:3904 次
发布时间:2019-05-23

本文共 2364 字,大约阅读时间需要 7 分钟。

实现原理

前端发送请求的时候需要自带一个action参数,用于寻找Servlet中是否存在这个方法。

建立一个Servlet基类,并重写service方法,主要实现判断自己的这个类中是否存在action参数值中对等的方法,如果有调用invoke方法执行这个方法。

其他的Servlet继承这个基类,并写下不同请求所对应的不同方法,因为继承基类,所以当前端发送请求时会自动执行基类中的service方法,因此就相当于在这个Servlet类中是否存在action参数值的方法。

代码展示

基类:

import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.lang.reflect.Method;public class BaseServlet extends HttpServlet {
protected void service (HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding("utf-8"); // 接收前端发送的action参数 String action = request.getParameter("action"); // 获取类的字节码 Class clazz = this.getClass(); Method method = null; try {
// 获取类中的方法 method = clazz.getMethod(action, HttpServletRequest.class, HttpServletResponse.class); if(method!=null) {
// 调用方法 String desPath = (String)method.invoke(this,request,response); if(desPath!=null) {
// 指执行完方法后跳转的位置 request.getRequestDispatcher(desPath).forward(request,response); } } } catch (Exception e) {
e.printStackTrace(); } }}

测试Servlet类:

import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet("/TestServlet")public class TestServlet extends BaseServlet {
// 处理请求1 public String Test1(HttpServletRequest request,HttpServletResponse response) {
System.out.println("This is Test1"); return "/TestServlet?action=Test2"; } // 处理请求2 public String Test2(HttpServletRequest request,HttpServletResponse response) {
System.out.println("This is Test2"); return "index.jsp"; }}

前端:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>      $Title$      click me  

此测试是index向Test1发送请求,然Test1处理完后转发到Test2,Test2再转发到index

运行结果:
在这里插入图片描述
在这里插入图片描述

转载地址:http://sdaen.baihongyu.com/

你可能感兴趣的文章
RedHat + OS CPU、MEM、DISK
查看>>
project bbs_discuz
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>
Unix + OS books
查看>>
script webshell jspWebShell / pythonWebShell / phpWebShell
查看>>
project site_dns
查看>>
webServer kzserver/1.0.0
查看>>
hd printer lexmark / dazifuyin / dayin / fuyin
查看>>
OS + Unix IBM Aix basic / topas / nmon / filemon / vmstat / iostat / sysstat/sar
查看>>
monitorServer nagios / cacti / tivoli / zabbix / SaltStack
查看>>
my ReadMap subway / metro / map / ditie / gaotie / traffic / jiaotong
查看>>
OS + Linux DNS Server Bind
查看>>
web test flow
查看>>
web test LoadRunner SAP / java / Java Vuser / web_set_max_html_param_len
查看>>
OS + UNIX AIX command
查看>>
OS + UNIX AIX performance
查看>>
OS + UNIX AIX Tools
查看>>
my ReadBook_liutongjingjixue / circulation economics
查看>>
my ReadBook_wangluoyingxiaoyucehua / network marketing / wangluoyingxiao
查看>>
db base database
查看>>